package com.sky.controller.user;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.WxDishService;
import com.sky.vo.DishVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
@RestController("userDishController")
@RequestMapping("/user/dish")
public class DishController {

    @Autowired
    WxDishService wxDishService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/list")
    public Result<List<DishVO>> queryList(@RequestParam Long categoryId) throws JsonProcessingException {
        System.out.println(categoryId);
        String key = "DISH_"+categoryId;
//        redis 缓存判断
        // 反序列化
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        // 有 -> 使用redis
        if(list != null){
            return Result.success(list);
        }
        //无 -> 查mysql 并设置redis
        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);
        list = wxDishService.queryListByCategoryDish(dish);
        redisTemplate.opsForValue().set(key, list);
        return Result.success(list);
    }
}
